﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MxWeiXinPF.DBUtility;
using MxWeiXinPF.Common;//Please add references
namespace MxWeiXinPF.DAL
{
	/// <summary>
	/// 数据访问类:wxapp_appinfo
	/// </summary>
	public partial class wxapp_appinfo
	{
		public wxapp_appinfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "wxapp_appinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from wxapp_appinfo");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(MxWeiXinPF.Model.wxapp_appinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into wxapp_appinfo(");
			strSql.Append("wxappName,wxappRequirement,wxappPic,tyQRPic,wxappBrief,oldPrice,currPrice,validYear,validDate,status,appName,pageUrl,appId,appSecret,createTime,createPerson,updateTime,shelfTime,wxappShopId)");
			strSql.Append(" values (");
			strSql.Append("@wxappName,@wxappRequirement,@wxappPic,@tyQRPic,@wxappBrief,@oldPrice,@currPrice,@validYear,@validDate,@status,@appName,@pageUrl,@appId,@appSecret,@createTime,@createPerson,@updateTime,@shelfTime,@wxappShopId)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@wxappName", SqlDbType.VarChar,150),
					new SqlParameter("@wxappRequirement", SqlDbType.VarChar,2000),
					new SqlParameter("@wxappPic", SqlDbType.VarChar,300),
					new SqlParameter("@tyQRPic", SqlDbType.VarChar,500),
					new SqlParameter("@wxappBrief", SqlDbType.VarChar,300),
					new SqlParameter("@oldPrice", SqlDbType.Decimal,9),
					new SqlParameter("@currPrice", SqlDbType.Decimal,9),
					new SqlParameter("@validYear", SqlDbType.Int,4),
					new SqlParameter("@validDate", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@appName", SqlDbType.VarChar,100),
					new SqlParameter("@pageUrl", SqlDbType.VarChar,500),
					new SqlParameter("@appId", SqlDbType.VarChar,100),
					new SqlParameter("@appSecret", SqlDbType.VarChar,100),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@createPerson", SqlDbType.VarChar,50),
					new SqlParameter("@updateTime", SqlDbType.DateTime),
					new SqlParameter("@shelfTime", SqlDbType.DateTime),
                    new SqlParameter("@wxappShopId", SqlDbType.VarChar,20)};
			parameters[0].Value = model.wxappName;
			parameters[1].Value = model.wxappRequirement;
			parameters[2].Value = model.wxappPic;
			parameters[3].Value = model.tyQRPic;
			parameters[4].Value = model.wxappBrief;
			parameters[5].Value = model.oldPrice;
			parameters[6].Value = model.currPrice;
			parameters[7].Value = model.validYear;
			parameters[8].Value = model.validDate;
			parameters[9].Value = model.status;
			parameters[10].Value = model.appName;
			parameters[11].Value = model.pageUrl;
			parameters[12].Value = model.appId;
			parameters[13].Value = model.appSecret;
			parameters[14].Value = model.createTime;
			parameters[15].Value = model.createPerson;
			parameters[16].Value = model.updateTime;
			parameters[17].Value = model.shelfTime;
            parameters[18].Value = model.wxappShopId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(MxWeiXinPF.Model.wxapp_appinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update wxapp_appinfo set ");
			strSql.Append("wxappName=@wxappName,");
			strSql.Append("wxappRequirement=@wxappRequirement,");
			strSql.Append("wxappPic=@wxappPic,");
			strSql.Append("tyQRPic=@tyQRPic,");
			strSql.Append("wxappBrief=@wxappBrief,");
			strSql.Append("oldPrice=@oldPrice,");
			strSql.Append("currPrice=@currPrice,");
			strSql.Append("validYear=@validYear,");
			strSql.Append("validDate=@validDate,");
			strSql.Append("status=@status,");
			strSql.Append("appName=@appName,");
			strSql.Append("pageUrl=@pageUrl,");
			strSql.Append("appId=@appId,");
			strSql.Append("appSecret=@appSecret,");
			strSql.Append("createTime=@createTime,");
			strSql.Append("createPerson=@createPerson,");
			strSql.Append("updateTime=@updateTime,");
			strSql.Append("shelfTime=@shelfTime,");
            strSql.Append("wxappShopId=@wxappShopId");
            strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@wxappName", SqlDbType.VarChar,150),
					new SqlParameter("@wxappRequirement", SqlDbType.VarChar,2000),
					new SqlParameter("@wxappPic", SqlDbType.VarChar,300),
					new SqlParameter("@tyQRPic", SqlDbType.VarChar,500),
					new SqlParameter("@wxappBrief", SqlDbType.VarChar,300),
					new SqlParameter("@oldPrice", SqlDbType.Decimal,9),
					new SqlParameter("@currPrice", SqlDbType.Decimal,9),
					new SqlParameter("@validYear", SqlDbType.Int,4),
					new SqlParameter("@validDate", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@appName", SqlDbType.VarChar,100),
					new SqlParameter("@pageUrl", SqlDbType.VarChar,500),
					new SqlParameter("@appId", SqlDbType.VarChar,100),
					new SqlParameter("@appSecret", SqlDbType.VarChar,100),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@createPerson", SqlDbType.VarChar,50),
					new SqlParameter("@updateTime", SqlDbType.DateTime),
					new SqlParameter("@shelfTime", SqlDbType.DateTime),
                    new SqlParameter("@wxappShopId", SqlDbType.VarChar,20),
                    new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.wxappName;
			parameters[1].Value = model.wxappRequirement;
			parameters[2].Value = model.wxappPic;
			parameters[3].Value = model.tyQRPic;
			parameters[4].Value = model.wxappBrief;
			parameters[5].Value = model.oldPrice;
			parameters[6].Value = model.currPrice;
			parameters[7].Value = model.validYear;
			parameters[8].Value = model.validDate;
			parameters[9].Value = model.status;
			parameters[10].Value = model.appName;
			parameters[11].Value = model.pageUrl;
			parameters[12].Value = model.appId;
			parameters[13].Value = model.appSecret;
			parameters[14].Value = model.createTime;
			parameters[15].Value = model.createPerson;
			parameters[16].Value = model.updateTime;
			parameters[17].Value = model.shelfTime;
            parameters[18].Value = model.wxappShopId;
            parameters[19].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wxapp_appinfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wxapp_appinfo ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.wxapp_appinfo GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,wxappName,wxappRequirement,wxappPic,tyQRPic,wxappBrief,oldPrice,currPrice,validYear,validDate,status,appName,pageUrl,appId,appSecret,createTime,createPerson,updateTime,shelfTime,wxappShopId from wxapp_appinfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			MxWeiXinPF.Model.wxapp_appinfo model=new MxWeiXinPF.Model.wxapp_appinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.wxapp_appinfo DataRowToModel(DataRow row)
		{
			MxWeiXinPF.Model.wxapp_appinfo model=new MxWeiXinPF.Model.wxapp_appinfo();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["wxappName"]!=null)
				{
					model.wxappName=row["wxappName"].ToString();
				}
				if(row["wxappRequirement"]!=null)
				{
					model.wxappRequirement=row["wxappRequirement"].ToString();
				}
				if(row["wxappPic"]!=null)
				{
					model.wxappPic=row["wxappPic"].ToString();
				}
				if(row["tyQRPic"]!=null)
				{
					model.tyQRPic=row["tyQRPic"].ToString();
				}
				if(row["wxappBrief"]!=null)
				{
					model.wxappBrief=row["wxappBrief"].ToString();
				}
				if(row["oldPrice"]!=null && row["oldPrice"].ToString()!="")
				{
					model.oldPrice=decimal.Parse(row["oldPrice"].ToString());
				}
				if(row["currPrice"]!=null && row["currPrice"].ToString()!="")
				{
					model.currPrice=decimal.Parse(row["currPrice"].ToString());
				}
				if(row["validYear"]!=null && row["validYear"].ToString()!="")
				{
					model.validYear=int.Parse(row["validYear"].ToString());
				}
				if(row["validDate"]!=null && row["validDate"].ToString()!="")
				{
					model.validDate=DateTime.Parse(row["validDate"].ToString());
				}
				if(row["status"]!=null && row["status"].ToString()!="")
				{
					model.status=int.Parse(row["status"].ToString());
				}
				if(row["appName"]!=null)
				{
					model.appName=row["appName"].ToString();
				}
				if(row["pageUrl"]!=null)
				{
					model.pageUrl=row["pageUrl"].ToString();
				}
				if(row["appId"]!=null)
				{
					model.appId=row["appId"].ToString();
				}
				if(row["appSecret"]!=null)
				{
					model.appSecret=row["appSecret"].ToString();
				}
				if(row["createTime"]!=null && row["createTime"].ToString()!="")
				{
					model.createTime=DateTime.Parse(row["createTime"].ToString());
				}
				if(row["createPerson"]!=null)
				{
					model.createPerson=row["createPerson"].ToString();
				}
				if(row["updateTime"]!=null && row["updateTime"].ToString()!="")
				{
					model.updateTime=DateTime.Parse(row["updateTime"].ToString());
				}
				if(row["shelfTime"]!=null && row["shelfTime"].ToString()!="")
				{
					model.shelfTime=DateTime.Parse(row["shelfTime"].ToString());
				}

                if (row["wxappShopId"] != null)
                {
                    model.wxappShopId = row["wxappShopId"].ToString();
                }
            }
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,wxappName,wxappRequirement,wxappPic,tyQRPic,wxappBrief,oldPrice,currPrice,validYear,validDate,status,appName,pageUrl,appId,appSecret,createTime,createPerson,updateTime,shelfTime,wxappShopId ");
			strSql.Append(" FROM wxapp_appinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,wxappName,wxappRequirement,wxappPic,tyQRPic,wxappBrief,oldPrice,currPrice,validYear,validDate,status,appName,pageUrl,appId,appSecret,createTime,createPerson,updateTime,shelfTime,wxappShopId ");
			strSql.Append(" FROM wxapp_appinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM wxapp_appinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from wxapp_appinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "wxapp_appinfo";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  '' status_s, *  from wxapp_appinfo   ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where  " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        ///  
        /// </summary>
        public bool DeleteWxappById(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wxapp_appinfo where   id=" + id + " and status!=1 and  id not in (select wxappId from wxapp_userList )  ");
           
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 与购买表一起查询
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataSet GetListByUsed(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select '' status_s,app.*,u.validDate usedvalidDate,u.status  usedStatus  from wxapp_appinfo app right join wxapp_userList u on app.id=u.wxappId
where  app.status=1
   ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and  " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }


		#endregion  ExtensionMethod
	}
}

